package com.vhall.component.service.account.impl;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;
import com.vhall.component.dao.account.AccessMapper;
import com.vhall.component.entity.account.vo.AccessListVO;
import com.vhall.component.entity.account.vo.AccessTokenVO;
import com.vhall.component.plugin.common.constant.HttpConstant;
import com.vhall.component.plugin.common.constant.PaasCommonConstant;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.plugin.core.service.VssTokenService;
import com.vhall.component.service.account.AccessService;
import com.vhall.component.service.account.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rui.zhou
 * @date 2021/4/21 15:02
 */
@Service
public class AccessServiceImpl implements AccessService {

    @Autowired
    private AccessMapper accessMapper;

    @Autowired
    private PaasService paasService;

    @Autowired
    private VssTokenService vssTokenService;

    /**
     * 获取权限列表
     *
     * @param reqVO
     * @return
     */
    @Override
    public List<Integer> getAccessList(AccessListVO reqVO) {
        String roleName = reqVO.getRoleName();
        List<Integer> rules;
        List<Integer> userRemove = new ArrayList<>();
        rules = accessMapper.selectAllRules();
        if (CommonConstant.GUEST.equals(roleName)) {
            userRemove.add(CommonConstant.GUEST_REMOVE);
            userRemove.add(CommonConstant.USER_REMOVE3);
            rules.removeAll(userRemove);
        }
        if (CommonConstant.USER.equals(roleName)) {
            userRemove.add(CommonConstant.GUEST_REMOVE);
            userRemove.add(CommonConstant.USER_REMOVE1);
            userRemove.add(CommonConstant.USER_REMOVE2);
            userRemove.add(CommonConstant.USER_REMOVE3);
            rules.removeAll(userRemove);
        }
        if (CommonConstant.ASSISTANT.equals(roleName)) {
            userRemove.add(CommonConstant.ASSISTANT_REMOVE);
            rules.removeAll(userRemove);
        }
        return rules;
    }

    /**
     * 获取token
     *
     * @param vo
     * @return
     */
    @Override
    public JSONObject getAccessToken(Integer accountId, AccessTokenVO vo) {

        Map<String, Object> param = new HashMap<>(16);
        param.put("data_collect_manage", "all");
        param.put("data_collect_submit", "all");
        param.put("data_collect_view", "all");
        param.put(PaasCommonConstant.THIRD_PARTY_USER_ID_KEY, accountId);
        JSONObject result = paasService.createAccessToken(param);
        String token = "";
        if (result.getInteger(HttpConstant.CODE) == HttpStatus.HTTP_OK) {
            token = result.getJSONObject(HttpConstant.DATA).getString("access_token");
        }
        JSONObject resp = new JSONObject();
        resp.put("access_token", token);
        resp.put("account_id", accountId);
        resp.put("app_id", paasService.getAppId());
        return resp;
    }


    @Override
    public JSONObject getVssToken(Integer accountId) {
        //创建一个  vssToken
        String vssToken = vssTokenService.createToken(accountId);
        JSONObject resp = new JSONObject();
        resp.put("vss_token", vssToken);
        return resp;
    }
}
